﻿#region*********CopyRight*************************************
/**
 * 文件名:system_material_list
 * 注  释:材料清单页面代码
 * 作  者:谢文明
 * 创建日期: 2010-1-17
 * 修改既要：
 * ---日期--------修改原因------------修改人-------
 *    
 * */
#endregion****************************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using ModelInfo;
//using SQLDAL;
using DataBaseAccessLib;
using BusinessModel;
using BusinessModel.EntityModel;
using System.Collections.Generic;

namespace SDZJ
{
    public partial class DemoForm : System.Web.UI.Page
    {
        #region********变量定义**************
        /// <summary>
        /// 当前页面
        /// </summary>
        public int currentPage = 1;

        /// <summary>
        /// 查询条件
        /// </summary>
        public string condition = "";
        /// <summary>
        /// 实例化操作类
        /// </summary>
        MaterialHelper cMaterailList = new MaterialHelper();
        /// <summary>
        ///日志信息实体
        /// </summary>
        LogInfo logInfo = new LogInfo();
        /// <summary>
        ///日志信息操作类
        /// </summary>
        LogHelper cLog = new LogHelper();
        /// <summary>
        /// 用户id  
        /// </summary>
        public string currentUserID = "";
        /// <summary>
        /// 用户名称
        /// </summary>
        private string currentUserName = "";
        #endregion***************************

        #region******方法**********
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="condition"></param>
        private void GetDataSource(String condition)
        {
            //排序字段
            string sortExpression = "";
            //排序方式
            string sortDirection = "";
            if (this.gvInfo.Attributes["SortExpression"] != null)
            {
                sortExpression = this.gvInfo.Attributes["SortExpression"];
                sortDirection = this.gvInfo.Attributes["SortDirection"];
            }
            this.AspNetPager.PageChanged -= AspNetPager_PageChanged;

            condition = " where companyType like '%" + tbxQuery.Text + "%' or materialType like '%" + tbxQuery.Text + "%' or materialContent like '%" + tbxQuery.Text + "%'";
            EntityOprHelper mh = new EntityOprHelper();
            List<MaterialListInfo> list = mh.GetEntityList<MaterialListInfo>(AspNetPager.PageSize, currentPage, AspNetPager, sortExpression, sortDirection, condition);
           // DataTable table = cMaterailList.GetAllMaterailList(AspNetPager.PageSize, currentPage, AspNetPager, sortExpression, sortDirection, condition);
            if (list != null)
            {
                gvInfo.DataSource = list;//.DefaultView;
                gvInfo.DataBind();
            }

            if (currentPage > 1)
            {
                AspNetPager.FirstPageText = "<img src=../images/page03.gif border=0 valign=middle hspace=3>";
                AspNetPager.PrevPageText = "<img src=../images/page01.gif border=0 valign=middle hspace=3>";
            }
            else
            {
                AspNetPager.FirstPageText = "<img src=../images/page07.gif border=0 valign=middle hspace=3>";
                AspNetPager.PrevPageText = "<img src=../images/page05.gif border=0 valign=middle hspace=3>";
            }

            if (currentPage == AspNetPager.PageCount || AspNetPager.PageCount == 0)
            {
                AspNetPager.LastPageText = "<img src=../images/page08.gif border=0 valign=middle hspace=3>";
                AspNetPager.NextPageText = "<img src=../images/page06.gif border=0 valign=middle hspace=3>";
            }
            else
            {
                AspNetPager.LastPageText = "<img src=../images/page04.gif border=0 valign=middle hspace=3>";
                AspNetPager.NextPageText = "<img src=../images/page02.gif border=0 valign=middle hspace=3>";
            }

            this.AspNetPager.PageChanged += AspNetPager_PageChanged;
        }



        #endregion*****************

        #region******事件**********
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //防止SQL注入
            AvertInjection.OnLoadCheck();
            //登录用户信息
            if (Session["currentUserID"] != null)
            {
                currentUserID = Session["currentUserID"].ToString();
            }
            if (Session["UserName"] != null)
            {
                currentUserName = Session["UserName"].ToString();
            }


            if (!IsPostBack)
            {
                EntityOprHelper mh = new EntityOprHelper();

                //MaterialListInfo info = new MaterialListInfo();
                //mh.GetEntity<MaterialListInfo>(info, "320GK2CO 8F2A7P3KTTNTLIE0S791XAPI49F");

                //List<MaterialListInfo> info1 = mh.GetEntityList<MaterialListInfo>("");
                this.gvInfo.Attributes.Add("SortExpression", "companyType");//排序字段
                this.gvInfo.Attributes.Add("SortDirection", "asc");//排序方式
                GetDataSource(condition);
                //string aa = "aa";
            }
        }

        /// <summary>
        /// 分页控件翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            currentPage = AspNetPager.CurrentPageIndex; // 记录列表当前页码
            txtCurrentPage.Value = currentPage.ToString();
            GetDataSource(condition);// 绑定所有信息
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression.ToString();
            string sortDirection = "ASC";
            if (sortExpression == this.gvInfo.Attributes["SortExpression"])
            {
                sortDirection = (this.gvInfo.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            this.gvInfo.Attributes["SortExpression"] = sortExpression;
            this.gvInfo.Attributes["SortDirection"] = sortDirection;
            GetDataSource(condition);
        }
        /// <summary>
        /// 删除事件
        /// </summary>   
        protected void gvInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            try
            {
                string strID = gvInfo.DataKeys[e.RowIndex].Value.ToString();//获取主键列的值
                Boolean isSuccess = false;
                isSuccess = cMaterailList.DelRow(strID);
                if (isSuccess)
                {
                    //日志记录
                    logInfo.UserGUID = currentUserID;
                    logInfo.LogContent = "用户【" + currentUserName + "】在" + DateTime.Now.ToString() + " 删除材料清单记录";
                    logInfo.LogDate = DateTime.Now.ToString();
                    logInfo.LogID = OConfig.GetGUID();
                    logInfo.LogModal = "系统管理-删除材料清单记录";
                    cLog.AddLog(logInfo, this);
                    OConfig.AlertFaileSourceWindow("数据删除成功！", this);
                }
                else
                {
                    OConfig.AlertFaileSourceWindow("数据删除失败！", this);
                }

            }
            catch (Exception exp)
            {
                OConfig.AlertFaileSourceWindow("数据删除失败！", this);
            }
            GetDataSource(condition);
        }



        /// <summary>
        /// 行绑定事件
        /// </summary>   
        protected void gvInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtn = (ImageButton)e.Row.FindControl("btnDelete");
                Label lbl = (Label)e.Row.FindControl("lblID");

                //获取该行主键
                string str = "您确认要删除序号为【" + lbl.Text + "】的信息吗";
                imgbtn.Attributes.Add("OnClick", "return confirm('" + str + "');");

            }
        }

        /// <summary>
        /// 刷新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            GetDataSource(condition);
        }
        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            GetDataSource(condition);
        }

        #endregion*****************
    }
}